package day4;

public class Test5 {
    //二分查找
    public static int search(int[] arr,int num){
        int start = 0;
        int end = arr.length-1;
        int mid ;
        while (true){
            mid = (start+end)/2;
            if (num == arr[mid]){
                return mid;
            }
            if(num>arr[mid]){
                start = mid+1;
            }else {
                end = mid-1;
            }

            if(start>end){
                break;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        //二分查找
        int[] arr = {1, 3, 4, 5, 7, 9, 10};
        int dex = search(arr, 3);
        System.out.println("3的下标为："+dex);
    }
}
